﻿
@{
    ViewBag.Title = "AuthorityView";
    Layout = "~/Views/Shared/_MyLayout.cshtml";
}

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>权限管理</legend>
</fieldset>
<div>
    <form class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label" style="width:150px;">用户管理</label>
            <div class="layui-input-block" id="UserAuth">
                <input type="checkbox" name="UserAuth" title="用户管理" value="用户管理" lay-filter="CheckboxFilter">
            </div>

        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width:150px;">流程审批权限</label>
            <div class="layui-input-block" id="ProcessAuth">
                <input type="checkbox" name="ProcessAuth" title="节点管理" value="节点管理" lay-filter="CheckboxFilter">
                <input type="checkbox" name="ProcessAuth" title="工程部" value="工程部" lay-filter="CheckboxFilter">
                <input type="checkbox" name="ProcessAuth" title="业务部" value="业务部" lay-filter="CheckboxFilter">
                <input type="checkbox" name="ProcessAuth" title="行政部" value="行政部" lay-filter="CheckboxFilter">
                <input type="checkbox" name="ProcessAuth" title="技经部" value="技经部" lay-filter="CheckboxFilter">
                <input type="checkbox" name="ProcessAuth" title="市场部" value="市场部" lay-filter="CheckboxFilter">
                <input type="checkbox" name="ProcessAuth" title="财务部" value="财务部" lay-filter="CheckboxFilter">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width:150px;">主业阶段管理</label>
            <div class="layui-input-block" id="ZYArrange">
                <input type="checkbox" name="ZYArrange" title="项目前期" value="1" lay-filter="CheckboxFilter">
                <input type="checkbox" name="ZYArrange" title="前期测算" value="2" lay-filter="CheckboxFilter">
                <input type="checkbox" name="ZYArrange" title="项目实施" value="3" lay-filter="CheckboxFilter">
                <input type="checkbox" name="ZYArrange" title="竣工结算" value="4" lay-filter="CheckboxFilter">
                <input type="checkbox" name="ZYArrange" title="决算收款" value="5" lay-filter="CheckboxFilter">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width:150px;">用户工程阶段管理</label>
            <div class="layui-input-block" id="YHGCArrange">
                <input type="checkbox" name="YHGCArrange" title="项目前期" value="1" lay-filter="CheckboxFilter">
                <input type="checkbox" name="YHGCArrange" title="前期测算" value="2" lay-filter="CheckboxFilter">
                <input type="checkbox" name="YHGCArrange" title="项目实施" value="3" lay-filter="CheckboxFilter">
                <input type="checkbox" name="YHGCArrange" title="竣工结算" value="4" lay-filter="CheckboxFilter">
                <input type="checkbox" name="YHGCArrange" title="决算收款" value="5" lay-filter="CheckboxFilter">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <button type="button" class="layui-btn" style="float:left;" id="BtnSave"><i class="layui-icon">&#xe61f;</i>保存</button>
        </div>

    </form>
</div>



<script>
    var UserId = "@ViewBag.UserId";
    //var AuthList = JSON.parse('@Html.Raw(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(ViewBag.AuthList))');
    var AuthList = JSON.parse('@Html.Raw(Json.Encode(ViewBag.AuthList))');
    layui.use([ 'form', 'laydate'], function(){
        var form = layui.form;
        var laydate = layui.laydate;
        var IsValidate = 0;

        form.on('checkbox(CheckboxFilter)', function (data) {
            //console.log(data.elem); //得到checkbox原始DOM对象
            //console.log(data.elem.checked); //是否被选中，true或者false
            //console.log(data.value); //复选框value值，也可以通过data.elem.value得到
            //console.log(data.othis); //得到美化后的DOM对象

            var a = data.elem.checked;
            if (a == true) {
                $(data.elem).attr("checked", "checked");
            }
            else {
                $(data.elem).removeAttr("checked");
            }
            form.render('checkbox');


        });


        //更新复选框
        for (var i = 0; i < AuthList.length; i++) {
            
            $("#" + AuthList[i].Type + " input[value='" + AuthList[i].Name + "']").attr("checked", "checked");
        }
        form.render();


        $("#BtnSave").on("click", function () {
            var Authobj = [];
            var tempobj = {};
            form.render();

            //用户管理获取值
            $("#UserAuth input:checkbox").each(function () {
                if ($(this).attr("checked") == "checked") {
                    tempobj = {};
                    tempobj.Type = "UserAuth";
                    tempobj.Name = $(this).attr("value");
                    tempobj.UserId = UserId;
                    Authobj.push(tempobj);
                }
            });
            //项目流程获取值
            $("#ProcessAuth input:checkbox").each(function () {
                if ($(this).attr("checked") == "checked") {
                    tempobj = {};
                    tempobj.Type = "ProcessAuth";
                    tempobj.Name = $(this).attr("value");
                    tempobj.UserId = UserId;
                    Authobj.push(tempobj);
                }
            });
            //主业阶段部署
            $("#ZYArrange input:checkbox").each(function () {
                if ($(this).attr("checked") == "checked") {
                    tempobj = {};
                    tempobj.Type = "ZYArrange";
                    tempobj.Name = $(this).attr("value");
                    tempobj.UserId = UserId;
                    Authobj.push(tempobj);
                }
            });
            //用户工程阶段部署
            $("#YHGCArrange input:checkbox").each(function () {
                if ($(this).attr("checked") == "checked") {
                    tempobj = {};
                    tempobj.Type = "YHGCArrange";
                    tempobj.Name = $(this).attr("value");
                    tempobj.UserId = UserId;
                    Authobj.push(tempobj);
                }
            });


            //这里判断下是否没有一个权限选中
            if (Authobj.length == 0) {
                tempobj = {};
                tempobj.Type = "NoAuth";
                tempobj.Name = "";
                tempobj.UserId = UserId;
                Authobj.push(tempobj);
            }

            //return;
            $.ajax({
                type: "POST",
                url: "/Authority/AuthSave",
                async: false,
                contentType: "application/json",
                dataType: "json",
                //data: { "UserId": UserId, "Authobj": Authobj },
                data: JSON.stringify(Authobj),
                success: function (msg) {
                    if (msg.result == "OK") {
                        parent.layer.msg('修改成功', { icon: 6, time: 1000 });
                        //parent.table.reload("demo");
                        //var index = parent.layer.getFrameIndex(window.name);
                        //parent.layer.close(index);

                        parent.layui.admin.events.closeThisTabs();
                    } else {
                        alert(msg.result);
                    }
                }
            });

        });

    });
</script>

